//
//  ResetPasswordViewController.swift
//  ezyform
//
//  Created by MoennigQ on 19/02/2017.
//  Copyright © 2017 Client.Solution Pty Ltd. All rights reserved.
//

import UIKit

class ResetPasswordViewController: ModelBaseViewController {
    
    let customView = ResetPasswordView()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view = customView
        self.customView.setTitle(R.string.localizable.changePassword())
        
        self.customView.closeBtn.addTarget(self, action: #selector(dismissVC), for: UIControl.Event.touchUpInside)
        self.customView.saveClosure = {[weak self] in
            guard let strongSelf = self else { return  }
            strongSelf.saveData()
        }
    }
    
    func saveData() {
        let model = MyInformationModel()
        let oldPwd = self.customView.oldPwdTextField.textField.text ?? ""
        let newPwd = self.customView.newPwdTextField.textField.text ?? ""
        let newPwdConfirm = self.customView.confirmPwdTextField.textField.text ?? ""
        
        model.oldPassword = oldPwd
        model.newPassword = newPwd
        model.confirmNewPassword = newPwdConfirm
        //set block
        let viewModel = MyInformationService()
        viewModel.setBlockAfterNetwork({ (result , _) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showAlertViewWithOneBtn(title: "Success",message: R.string.localizable.pleaseLoginWithNewPassword()) {
                LoginService.logOut()
            }
        }){ (code, error) -> Void in
            ModelViewUtil.shared.hideLoadingView()
            Tool().showAlertViewWithOneBtn(title: "Failed",message: error)
        }
        ModelViewUtil.shared.showLoadingView("Saving...")
        viewModel.resetPassword(model)

    }

}
